package com.isoft.permanprovidor.controller.sgman;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isoft.permanprovidor.bean.ResponseData;
import com.isoft.permanprovidor.controller.BaseController;
import com.isoft.permanprovidor.entity.sgman.ContractWorker;
import com.isoft.permanprovidor.service.sgman.ContractWorker_Service;
import com.isoft.permanprovidor.unit.FileUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.security.PublicKey;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/permanprovidor/contractworker")
public class ContractWorker_Controller extends BaseController {
    @Autowired
    ContractWorker_Service contractWorker_service;
    /*分页综合查询操作*/
    @PostMapping("/getpage")
    public ResponseData PageSynthSelect(Integer current , Integer size, String name, String department, String formalest , String personnelcategory , String grade, String mainposition, String gender , String educationlevel , String ismarried){
        return contractWorker_service.PageSynthSelect(current,size,name,department,formalest,personnelcategory,grade,mainposition,gender,educationlevel,ismarried);
    }
    /*新增合同工员工*/
    @PostMapping("/addworker")
    public ResponseData<Boolean> addWorker(ContractWorker contractWorker){
        return returnBoolean(contractWorker_service.addWorker(contractWorker));
    }
    /*批量导入合同工信息*/
    @Value("${myfile.user.contractWorker}")
    private String savePath;
    public ResponseData<String> bulkImport(@RequestParam("contractworker")MultipartFile file , HttpServletRequest request){
        boolean result = false;
        String fileUrl = null;
        if (null != file){
            String newName="contractworker.xlsx";
            fileUrl = FileUtil.uploadPhyPath(file , newName ,savePath , request);
            log.info("存入物理路径:" + fileUrl);
            if (null != fileUrl){
                try {
                    // 调用服务层方法处理上传的文件
                    contractWorker_service.bulkImport(fileUrl);
                    result = true;
                } finally {
                    // 无论成功或失败，尝试删除文件
                    File uploadedFile = new File(fileUrl);
                    if (uploadedFile.exists()) {
                        if (uploadedFile.delete()) {
                            log.info("暂存文件删除成功: " + fileUrl);
                        } else {
                            log.error("暂存文件删除失败: " + fileUrl);
                        }
                    }
                }
            }
        }
        log.info("文件上传路径:"+fileUrl);
        ResponseData<String> responseData = new ResponseData<>();
        responseData.setData(fileUrl);
        if (result){
            responseData.setErrorCode(200);
            responseData.setMsg("文件上传成功");
        }else {
            responseData.setErrorCode(605);
            responseData.setMsg("文件上传失败");
        }
        return responseData;
    }
    /*修改合同工信息*/
    @PutMapping("/update")
    public ResponseData<Boolean> updateWorker(@RequestBody ContractWorker contractWorker){
        return  returnBoolean(contractWorker_service.updateWorker(contractWorker));
    }
    /*启/停用该合同工*/
    @PostMapping("/freezeenable")
    public ResponseData<Boolean> islockWorker (Integer id ,String sysuserid,Integer islock){
        return returnBoolean(contractWorker_service.islockWorker(id, sysuserid, islock));
    }

}
